import type { Schema } from './types.ts';
import StringField from './fields/StringField.tsx';
import NumberField from './fields/NumberField.tsx';

export default defineComponent({
  name: 'SchemaItem',
  props: {
    schema: {
      type: Object as PropType<Schema>,
      required: true,
    },
    value: {
      required: true,
    },
    onChange: {
      type: Function as PropType<(v: any) => void>,
      required: true,
    },
  },
  setup(props) {
    return () => {
      const { schema } = props;

      // TODO 如果type没有指定，需要猜测type

      const type = schema.type;

      let Component: any;

      switch (type) {
        case 'string':
          Component = StringField;
          break;
        case 'number':
          Component = NumberField;
          break;
        default:
          console.error(`${type} is not supported`);
      }

      return <Component {...props} />;
    };
  },
});
